from windoh.control import Control
from windoh.controls.label import Label
from windoh.controls.button import Button


class DropDown(Control):

    class DropDownListItem(Label):
        def __init__(self, idx, *args, **kwargs):
            self._idx = idx
            Label.__init__(self, *args, **kwargs)

        def on_mouse_over(self):
            self.classname = "hover"
            self.set_dirty()

        def on_mouse_out(self):
            self.classname = None
            self.set_dirty()

        def on_mouse_release(self, x, y, b, m):
            print self, self._idx
            self._parent._owner.dispatch_event('on_select_item', self._idx)
            self._parent._owner.remove_win()

    class DropDownList(Control):
        def __init__(self, owner, **kwargs):
            Control.__init__(self, **kwargs)
            self._owner = owner

            for idx, opt in enumerate(owner.options):
                label = owner.DropDownListItem(idx, opt,
                    left=1, right=0, bottom=idx*20, height=20)
                self.append(label)
            self.style['height'] = (len(owner.options)*20)+1
            self.style['bottom'] -= (len(owner.options)*20)+1

    class DropDownRootHandler(object):
        def __init__(self, owner, win):
            self._owner = owner
            self._win = win

        def on_pre_mouse_press(self, *args):
            #print "nah mp"
            return True

        def on_pre_mouse_release(self, x, y, *args):
            cs = self._win._computed_style
            if (x > cs['left'] and 
                y > cs['bottom'] and 
                x < cs['left'] + cs['width'] and
                y < cs['bottom'] + cs['height']):
                return False

            self._owner.remove_win()
            #print "nah mr"
            return True

    class DropDownLabel(Label):
        pass

    class DropDownButton(Button):
        def on_mouse_press(self, x, y, b, m):
            pass

    def __init__(self, options=None, selected_index=0, **kwargs):
        Control.__init__(self, **kwargs)
        self.options = options or []
        self.selected_index = selected_index

        try:
            sel_text = self.options[selected_index]
        except IndexError:
            sel_text = ''

        self._label = self.DropDownLabel(str(sel_text), 
            left=1, top=1, bottom=1, right=20)
        self.append(self._label)
        self._button = self.DropDownButton( 
            top=1, bottom=1, right=1, width=18)
        self.append(self._button)

    def on_pre_mouse_release(self, x, y, b, m):
        cs = self._computed_style
        
        self._win = self.DropDownList(self, left=cs['screen_x'], bottom=cs['screen_y'],
            width=cs['width'])
        self._root.append(self._win)
        self._root.push_handlers(self.DropDownRootHandler(self, self._win))
        return True
        
    def remove_win(self):
        self._root.pop_handlers()
        self._root.remove(self._win)
        
    def on_select_item(self, idx):
        self.selected_index = idx
        try:
            sel_text = self.options[idx]
        except IndexError:
            sel_text = ''
        self._label.label = sel_text

DropDown.register_event_type('on_select_item')


